using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using NUnit.Framework;

namespace sCicero
{
    [TestFixture]
    public class sTest
    {
        private sCicero.scicero m_osCicero;
        private Data m_oData;

        [Test]
        public void TestStart()
        {
            try
            {
                string sDebugFile = "debug.d";
                System.IO.FileInfo oF = new System.IO.FileInfo(sDebugFile);
                if (oF.Exists)
                {
                    oF.IsReadOnly = false;
                    oF.Delete();
                }
                sCicero.GlobalEvent oTestGlobalEvent = new GlobalEvent();
                m_oData = new Data(ref oTestGlobalEvent, "debug.d");
                m_osCicero = new scicero(ref m_oData);
                sCicero.update oUpd = new update(ref m_oData, ref m_osCicero);
            }
            catch (Exception ex)
            {
                Assert.AreEqual(1d, 2d);
            }
            

            /*
            Account source = new Account();
            source.Deposit(200.00F);
            Account destination = new Account();
            destination.Deposit(150.00F);

            source.TransferFunds(destination, 100.00F);
            Assert.AreEqual(250.00F, destination.Balance);
            Assert.AreEqual(100.00F, source.Balance);
             * */

        }
    }
    public partial class TestForm : Form
    {
        private sCicero.scicero m_osCicero;
        private Data m_oData;
        private bool bRunning;

        public TestForm()
        {
            InitializeComponent();

            timer_show.Enabled = false;
        }

        private string Result
        {
            set
            {
                if (bRunning)
                {
                    textBox_result.AppendText(DateTime.Now.ToLocalTime().ToShortTimeString() + " " + value + Environment.NewLine);
                    textBox_result.Select(textBox_result.Text.Length, 1);
                }
            }
        }

        private void TestForm_Load(object sender, EventArgs e)
        {


        }

        public void StartTest()
        {
            try
            {
                bRunning = true;
                Result = "Start of test";
                string sDebugFile = "debug.d";
                System.IO.FileInfo oF = new System.IO.FileInfo(sDebugFile);
                if (oF.Exists)
                {
                    oF.IsReadOnly = false;
                    oF.Delete();
                }
                Result = "GlobalEvent";
                sCicero.GlobalEvent oTestGlobalEvent = new GlobalEvent();
                Result = "Data";
                m_oData = new Data(ref oTestGlobalEvent, "debug.d");
                Result = "sCicero Core";
                m_osCicero = new scicero(ref m_oData);
                //update to the right version
                sCicero.update oUpd = new update(ref m_oData, ref m_osCicero);
            }
            catch (Exception ex)
            {
                Result = ex.Message;
                Result = ex.Source;
                Result = ex.StackTrace;
            }
            

            if (TestSource())
                timer_show.Enabled = true;
        }

        private bool TestSource()
        {

            Errorcode oErrorCode;
            int nPeriodId;
            int nClassId;
            int nStudentId;
            int nSubjectId;
            int nTestId;
            int nResultId;
            int nDocumentId;
            int nTemplateId;

            DataRow oDr;
            //start test
            //1 create period

            Result = "Create Period";
            oErrorCode = m_osCicero.Periods.New("TestPeriod", false);
            if (oErrorCode != Errorcode.NOERROR)
            {
                Result = m_osCicero.Periods.LastError() + " e:" + oErrorCode;
                return false;
            }
            else
            {
                nPeriodId = (int)m_osCicero.Periods.GetLastKey();
                Result = "Period success " + nPeriodId.ToString();
            }
            //2 create class
            Result = "Create Class";
            oErrorCode = m_osCicero.Classes.New("TestClass", nPeriodId);
            if (oErrorCode != Errorcode.NOERROR)
            {
                Result = m_osCicero.Classes.LastError() + " e:" + oErrorCode;
                return false;
            }
            else
            {
                nClassId = (int)m_osCicero.Classes.GetLastKey();
                Result = "Class success " + nClassId.ToString();
            }

            //3 create student
            Result = "Create Student";
            oErrorCode = m_osCicero.Students.New("test1", "Test2", nClassId);
            if (oErrorCode != Errorcode.NOERROR)
            {
                Result = m_osCicero.Students.LastError() + " e:" + oErrorCode;
                return false;
            }
            else
            {
                nStudentId = (int)m_osCicero.Students.GetLastKey();
                Result = "Student success " + nStudentId.ToString();
            }

            //4 create Subject
            Result = "Create Subject";
            oErrorCode = m_osCicero.Subjects.New("SubjectTest");
            if (oErrorCode != Errorcode.NOERROR)
            {
                Result = m_osCicero.Subjects.LastError() + " e:" + oErrorCode;
                return false;
            }
            else
            {
                nSubjectId = (int)m_osCicero.Subjects.GetLastKey();
                Result = "Subject success " + nSubjectId.ToString();
            }

            //5 create Test
            Result = "Create Test";
            oErrorCode = m_osCicero.Tests.New("", "Test1", nSubjectId, "Tag");
            if (oErrorCode != Errorcode.NOERROR)
            {
                Result = m_osCicero.Tests.LastError() + " e:" + oErrorCode;
                return false;
            }
            else
            {
                nTestId = (int)m_osCicero.Tests.GetLastKey();
                Result = "Test success " + nTestId.ToString();
            }

            //6 create Result
            Result = "Create Result";
            oErrorCode = m_osCicero.Results.New(nStudentId, "comment", System.DateTime.Now, 0, nTestId);
            if (oErrorCode != Errorcode.NOERROR)
            {
                Result = m_osCicero.Results.LastError() + " e:" + oErrorCode;
                return false;
            }
            else
            {
                nResultId = (int)m_osCicero.Results.GetLastKey();
                Result = "Result success " + nResultId.ToString();
            }

            //7 create Document
            Result = "Create Document";
            oErrorCode = m_osCicero.Documents.New("New doc", "Testdata", nStudentId);
            if (oErrorCode != Errorcode.NOERROR)
            {
                Result = m_osCicero.Documents.LastError() + " e:" + oErrorCode;
                return false;
            }
            else
            {
                nDocumentId = (int)m_osCicero.Documents.GetLastKey();
                Result = "Result success " + nDocumentId.ToString();
            }

            //8 edit Document
            Result = "Edit Document";
            //read doc into datarow
            oDr = m_osCicero.Documents.Get(nDocumentId);
            oErrorCode = m_osCicero.Documents.Edit(nDocumentId, oDr["name"] + "test", oDr["data"] + "test", nStudentId);
            if (oErrorCode != Errorcode.NOERROR)
            {
                Result = m_osCicero.Documents.LastError() + " e:" + oErrorCode;
                return false;
            }
            else
            {
                Result = "Result success ";
            }

            //9 edit student
            Result = "Edit Student";
            //read doc into datarow
            oDr = m_osCicero.Students.Get(nStudentId);
            oErrorCode = m_osCicero.Students.Edit(nStudentId, oDr["firstname"] + "test", oDr["lastname"] + "test", nClassId);
            if (oErrorCode != Errorcode.NOERROR)
            {
                Result = m_osCicero.Students.LastError() + " e:" + oErrorCode;
                return false;
            }
            else
            {
                Result = "Result success ";
            }

            //10 create template
            Result = "Create Template";
            oErrorCode = m_osCicero.Templates.New("TestTemplate", "TestData");
            if (oErrorCode != Errorcode.NOERROR)
            {
                Result = m_osCicero.Templates.LastError() + " e:" + oErrorCode;
                return false;
            }
            else
            {
                nTemplateId = (int)m_osCicero.Templates.GetLastKey();
                Result = "Result success ";
            }

            //11 tree
            Result = "Get Tree";
            XmlDocument oXml = m_osCicero.Tree.GetTree();
            if (oXml == null)
            {
                Result = m_osCicero.Tree.LastError() + " e:" + m_osCicero.Tree.ReturnCode();
                return false;
            }
            else
            {
                Result = "Result success ";
            }
            return true;

        }

        private void timer_show_Tick(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button_skip_Click(object sender, EventArgs e)
        {
            bRunning = false;
            this.Close();
        }

    }
}